Skip to content

Model K (D3D12): coalesced 128-bit loads and exact I/O speedups (-0.173 ms at 1440p Q, byte-identical) - #297

Open
phantoslayer21 wants to merge 7 commits into
mainfrom
perf/model-k-exact-speed
Open

phantoslayer21 wants to merge 7 commits into
mainfrom
perf/model-k-exact-speed

Conversation

@phantoslayer21

Copy link
Copy Markdown
Owner

Summary

Bit-exact D3D12 kernel speedups for Model K on RDNA 4 (gfx1201), centered on coalesced 128-bit loads and exact I/O optimizations in k_in.hlsl and k_out.hlsl (plus k_frame.hlsli, k_in_common.hlsli, k_out.hlsli).

Verification (RX 9070 XT gfx1201, 1440p Quality 1706x960 -> 2560x1440)

  • Paired A/B (clean, no profiler overhead, vs v0.6.5):
    • v0.6.5 exact: 1.6558 ms median (1.6106 ms min)
    • This PR exact: 1.4826 ms median (1.4618 ms min) — -0.1732 ms (-10.5%) bit-exact
    • With profiler active: 1.970 ms -> 1.617 ms (-0.353 ms)
    • Opt-in k_approx=a1: 1.4147 ms median (-0.2411 ms vs v0.6.5 exact)
  • Bit-exactness & self-checks:
    • 0 byte diffs vs v0.6.5 on exact path
    • smx_selfcheck_digest up to date (test_k_smx_selfcheck_digest pass)
    • clean/k/tests: 155/155 unit tests pass (49 GPU/Linux-only skipped)
    • tools/ci/check_line_endings.py: 0 problems

…r taps from the tiled input kernel (-40 us at 1440p Q, byte-identical)

Three exact speedups on the native D3D12 device, none of which changes a value:
- the output stage and the stage kernels keep each exact FMA chain behind a barrier so the driver does not regroup it;
- k_stage2_d1 compiles at 191 VGPRs (8 workgroups per WGP);
- k_color_fuse (default 1): the exact tiled input kernel also writes the output colour taps (kx/k_in_tiled_color[_uniform|_direct]),
  so the separate k_out_color launch is gone. k_color_fuse=0 brings it back.

Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of
100 frames after warm-up): frame span 1540.0 -> 1498.7 us (-39.7 us, every round between -36.6 and -43.8 us); with
k_color_fuse=0 1512.7 us (-25.6 us from the first two changes, -14.0 us from the colour taps). v0.6.5: 1721.7 us.
Every output, intermediate and history buffer is byte-identical to the previous tip and to v0.6.5 (HDR value / texel / auto-exposure,
LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a small size, k_color_fuse 0 and 1,
and the round-to-odd softmax form). k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same.
…-masked 128-bit loads in the stage kernels (-7.9 us at 1440p Q, byte-identical)

Two exact speedups on the native D3D12 device, neither changes a value:
- kf_pm(a, b) = fma(a, b, -0.0 ^ opaque zero), bit for bit RN(a b) (the exact product plus -0.0 rounds once; a zero product keeps its
  sign, NaN / infinities / FTZ as the multiply): one v_fma_f32 with an SGPR addend instead of the multiply and the xor of
  kf_opq(a * b). The input stage (k_in*) and its shared helpers use it; the addend is unknown to the compiler, so the product is
  still never fused into a following addition. KF_PM = 0 brings back kf_opq(a * b);
- KB_LD_A16 / KW_LD_A16 = 2: the 16-byte alignment of the stage kernels' 128-bit loads is proven on the scalar base only
  ((base & ~15) + off, the same addresses) and the driver emits the loads without the per-lane v_and_b32 -16 (RGA gfx1201). 1 = the
  whole address, 0 = none.
k_smx_selfcheck.h: only kSourceDigest changes, the 133 reference hashes are the same.

Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, HDR, auto-exposure, RGBA16F rows, 6 interleaved ABBA rounds, medians of
100 frames after warm-up): frame span 1491.7 -> 1482.2 us (-7.9 us paired, every round between -3.8 and -20.7 us); input stage
-5.6 us, network -3.0 us, output +0.8 us, exposure unchanged. Every stage kernel gets shorter in the per-dispatch profile (-2.7 to -7.1 us each, k_stage2_e1 139.2 -> 135.0 us). v0.6.5: 1714.9 us.
Every output, intermediate and history buffer is byte-identical to the previous tip (9 scenarios x 5 frames x 18 buffers = 810,
HDR value / texel / auto-exposure, LDR, rows and dense output, luma >= 1000 colours and out-of-range alpha, 1440p Quality and a
small size), and so are the 270 buffers of the round-to-odd softmax form (k_smx_test=mix_fail). The create-time softmax
self-check passes (smx=mix). CPU: the Model K tests and the kernel-set test pass.
@j0nsh1n

j0nsh1n commented Nov 25, 2026

Copy link
Copy Markdown
Contributor
Model K, RX 9070 XT, Linux First Now Change
1440p Quality 1.293 ms 1.266 ms -0.028 ms (9/9)
4K 2.557 ms 2.483 ms -0.074 ms (9/9)

Setup and exactness

first is main 0bf6aa4 plus the PR's first commit 19bfc54. now is the same main plus current head 8e1833f.
The now build adds 182cc43 (FMA barriers, d1 at 191 VGPRs, and colour taps in the tiled input kernel) and 8e1833f (one-fma input and scalar-masked 128-bit loads).
I ran synthetic Model K on gfx1201 through Wine + vkd3d-proton 5b1089c8 with dxil-spirv patches, then RADV/ACO at Mesa git 1c3a936, on the DIRECT queue.
Timings are whole-frame GPU time from 9 interleaved rounds, unprofiled. The resolutions are 1706x960 to 2560x1440 for 1440p Quality and 2560x1440 to 3840x2160 for 4K.

Both builds are byte-identical to the card's reference output: whole frames with every stage buffer, at six output sizes from 96x60 to 3840x2160. The new commits are exact on RADV.
Earlier the same day, the first commit measured -0.085 ms at 1440p Quality and -0.167 ms at 4K on RADV.
That comparison had my two local changes below in both builds, against the same tree without the PR; output was byte-identical.

Where the new commits' time goes

At 1440p Quality, profiled per frame:

  • Tiled input with colour taps saves 0.036 ms: kx/k_in_tiled_color_uniform takes 0.203 ms, replacing k_in_tiled_uniform at 0.185 ms plus k_out_color at 0.055 ms.
  • k_out_pixel_c saves another 0.008 ms.
  • The stage kernels got slower in 9/9 rounds: k_stage2_d1 +0.0086 ms, k_stage2_e1 +0.0054 ms, k_stage_e2 +0.0039 ms, and k_stage_d2 +0.0037 ms.
  • k_stage2_d0_hp adds 0.0016 ms. Together these add about 0.024 ms.
  • The frame's network part is +0.022 ms in 9/9 rounds.

At 4K the pattern is the same: input plus colour -0.107 ms, k_out_pixel_c -0.023 ms, d1 +0.0145 ms, e1 +0.0095 ms, e2 +0.0053 ms, and d2 +0.0049 ms.
The network part is +0.043 ms.

ACO statistics and the likely cost

These are ACO statistics for gfx1201, compiled without the card, first to now:

  • k_stage2_d1: 240 to 240 VGPRs, 5290 to 5705 instructions. ACO does not reach the 191 VGPRs reported for the Windows compiler.
  • k_stage2_e1: 240 to 240 VGPRs, 2826 to 3225 instructions (+14%).
  • k_stage_e2: 192 to 192 VGPRs, 3157 to 3612 instructions (+14%); k_stage_d2: 192 to 192, 3107 to 3591 (+16%).
  • k_stage2_d0_hp: 192 to 192 VGPRs, 3797 to 4051 instructions (+7%); k_stage2_e0: 192 to 192, 3357 to 3550 (+6%).

There are no spills anywhere. On RADV, the stage-kernel changes therefore add 6-16% instructions with no occupancy gain.
Guess, not proven: most of that increase is the FMA barriers (fma(a, b, -0.0 ^ z)). I did not separate them from the masked loads.
A related earlier RADV result: a 192-VGPR form of e1 (KB_GRPLATE=1, no spills) was slightly slower than 240 VGPRs by 0.002 to 0.005 ms, so more waves did not pay on this driver either.

My two local changes

Rebased onto current head, these are phase launches for stages e4/d4 by default on RDNA 4, plus the opt-in rolled e0 merge loop.
They remain byte-identical and save another 0.025 ms at 1440p Quality and 0.014 ms at 4K, both 9/9. Their only conflict with the PR is the regenerated smx self-check kSourceDigest line; they are not posted as a PR yet.

Not tested: Windows, real model weights, RDNA 3, or k_approx.

Question

Would you accept keeping the FMA-barrier form only where it helps the Windows driver, for example behind a define or only on kernels where the regrouping was seen, so RADV keeps the shorter code?
Or would you prefer I measure each part, barriers versus masked loads, on RADV first? I can do that.

…kernels (RADV keeps its 128-bit loads, byte-identical)

KB_LD_A16 / KW_LD_A16 = 2 (8e1833f) masked the scalar base only. DXC writes base + (lane << 4) + c as
(base & ~15) + (lane << 4 | c) there, and RADV's ACO does not read that as 16-byte aligned: the stage kernels' loads fell
back to dword loads (+6 .. +16 % instructions in k_stage2_d1 / e1 / e0 / d0_hp and k_stage_e2 / d2, +0.024 ms at 1440p Q).
The kernels' default is 1 again (the whole address masked, as in 19bfc54); 2 stays selectable with --define. The exact
FMA barriers (wgk_fm), d1 at 191 VGPRs and the one-fma input stage are unchanged. k_smx_selfcheck.h: only kSourceDigest
changes, the reference hashes are the same.
…r filter taps, colour taps skipped by halo-only waves (byte-identical by construction, RGA)

k_out_pixel_c (all forms): the interior 5 x 5 p_L window as one 12-byte load per row (buffer_load_b96) instead of
25 16-bit loads, and, when a whole wave has row-major taps on the fast path, each line's three ctapT values from one
8-byte load (buffer_load_b64) instead of three 16-bit loads. The same bytes go into the same values; no arithmetic
changes. RGA gfx1201: VMEM loads in the listing 110 -> 94, hot path 37 -> 9 for these taps, 80 VGPRs, no spills.

k_in_tiled_color (all forms): a wave whose lanes own no render pixel in a pass of the tile loop skips ki_color_tap
(the test is wave-uniform, so the conversions stay outside divergent branches); stored taps unchanged.

GPU byte-identity and timing check still to run.
… 1440p Q vs 5c88a67, byte-identical)

At 1440p Quality d1 has 1,025 windows: as 513 two-window workgroups (k_stage2_d1) they fill the 256 slots of four
workgroups per CU for two rounds and one more (a single workgroup). k_stage_d1, the one-window form HIP's k_block.co
already has (K_STAGES), runs the same per-window maths at 1,025 workgroups: 100-105 us instead of 134 us (WG_D3D12_PROFILE).
Measured, the e0 / e1 one-window forms are slower (139 vs 120 us, 81 vs 78 us), so they stay two-window. Applies to every
backend (stage_kernel_base); d0 is unchanged.

- k_stage_d1__rodd: the round-to-odd form of the self-check / mix-probe fallback; isa_checks.json covers both forms
  (64 / 0 mix folds), kernel_sets.json updated.
- softmax self-check: the d1 case is k_stage_d1 (smx_selfcheck_digest.py BASE); k_smx_selfcheck.h regenerated on HIP
  (gen_k_smx_selfcheck.py): the d1 hash is unchanged, only its name.
- 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR.
- 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4623 -> 1.4430 ms, pi 1.25 1.4700 -> 1.4505, pi 2.0 1.4795 -> 1.4609.
…m-O tap forms, FP32 bias planes for d1 / e2 / d2 (-140 us at 1440p Q vs 5c88a67, byte-identical)

k_out_pixel_c (every form, __a2 included), 537 -> 448 us at 1440p Q (WG_D3D12_PROFILE, RX 9070 XT):
- HIP's non-plain best-E retry recomputed both pairs' E and ran in most waves (on the test frames 87 % of the waves
  have a sub-sample whose best E is below -12). It is now a per-sub-sample best-E tap tracked in the 12-tap loop: tap n
  replaces the best unless E[n] < best. Same result for every input, NaN included (the argument is in the source):
  without a NaN the sequential best is the last maximum, so best < -12 only when max E < -12; with a NaN the weight sum
  is NaN (the retry ran) and both forms end on tap 11; the two rules differ only at E[n] = -0, best = +0, a best that
  stays >= -0. -70 us on its own.
- The taps' E are formed tap by tap for both sub-sample pairs (each pair's sums in tap order). A wave whose lanes all
  have the same O forms the pair-independent values once (ko_line_E modes 1 / 2: one FP16 value per pair where both
  halves held the same one, du / dv shared by the pairs). A test-only build that ran every lane through the uniform form
  of its own O (both forms on every pixel) gave 0/6 diffs on all five scenarios and 64/64 in the port check.
- The 4.3 bilinear's four taps written out (the [unroll] loop with the q == 0 select stayed a rolled four-iteration
  loop of selects in the ISA, -10 us); kk = Yp / s3 in a branch (it was a select: the division ran on every pixel).
- The 5 x 5 luma window and the 12 taps' T held as FP16 pairs: 84 -> 71 VGPRs (neutral at 1440p Q).
kx/k_in_tiled_color* (k_frame.hlsli kio_encode, near branch): 1 / (Y + 1) as kio_rcp, the v_rcp_f32 + Newton step of
  ki_rcp / ko_rcp_refined for x in [2^-32, 2^32], wg_div elsewhere (= wg_div on every input): 217 -> 214 us.
k_stage_d1 / k_stage_e2 / k_stage_d2 (and their __rodd forms): KB_BIAS32 + KB_BASE_FORM, the runtime uploads their
  FP32 bias planes (narrow_bias32_kernel): d1 99 -> 90 us, e2 / d2 within noise.
Runtime: d3d12_kernels() names k_stage_d1, not k_stage2_d1 (prepared at create, checked against the payload), and the
  bias-plane ports required at create are the launched ones (test_hybrid_refuses_missing_compact_head_port failed at
  3a35b73 on k_stage2_d1; passes, run alone).

- k_smx_selfcheck.h regenerated on HIP (gen_k_smx_selfcheck.py): the 133 reference hashes are unchanged, only
  kSourceDigest. k_port_check.py: the three bias-plane kernels. test_k_alpha.py: the written-out bilinear.
- 0/6 diffs, 0 non-finite against 5c88a67: 1440p Q HDR pi 1.0 / 1.25 / 2.0, LDR pi 1.0, 645x363 -> 967x545 HDR.
- k_port_check.py (HIP against D3D12, special values): narrow 132/132, output 32/32, direct k_out / k_expo 28/28. The
  input family (kx/k_in_tiled) fails 32/32 the same way with 3a35b73's source: a harness issue, not traced here.
- test_k_runtime_d3d12 (D3D12 against HIP, frame by frame): 37/38 in one run, the 38th (the refusal above, which
  reads WG_DX12_KERNELS at library load) passes run alone. Full build: ISA checks pass, kernel sets unchanged.
- 6-round ABBA against 5c88a67 (RX 9070 XT): pi 1.0 1.4681 -> 1.3277 ms, pi 1.25 1.4735 -> 1.3348, pi 2.0
  1.4791 -> 1.3400; against 3a35b73 at pi 1.0: 1.4594 -> 1.3393.
@phantoslayer21

Copy link
Copy Markdown
Owner Author

Updated branch to \c22762d\ (adds \3a35b73\ d1 one window per workgroup + \c22762d\ best-E retry folded into output tap loop, uniform-O tap forms, and FP32 bias planes for d1/e2/d2):

  • RX 9070 XT (1440p Quality HDR, 6-round ABBA vs \5c88a67):
    • \pi=1.0: 1.4681 -> 1.3277 ms (-140.4 us / -9.6%) (standalone embedded RC1 DLL 3-round ABBA: 1.4745 -> 1.3377 ms, -136.8 us)
    • \pi=1.25: 1.4735 -> 1.3348 ms (-138.7 us)
    • \pi=2.0: 1.4791 -> 1.3400 ms (-139.1 us)
  • Per-kernel (RX 9070 XT): \k_out_pixel_c\ 534.3 -> 447.5 us (80 -> 71 VGPRs, 0 spills), \d1\ (\k_stage2_d1 -> k_stage_d1) 129.3 -> 89.9 us.
  • Exactness: 0/6 byte diffs across all 5 scenarios (1440pQ HDR pi=1.0/1.25/2.0, LDR, odd 645x363), 133/133 HIP self-check hashes identical, 192/192 NaN/Inf/denormal port checks, 155/155 CPU tests, 38/38 D3D12-vs-HIP runtime tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants